﻿var isFF = (navigator.userAgent.indexOf("Firefox") != -1); //是否火狐
var isIE6 = (navigator.userAgent.indexOf("MSIE 6.0") > -1);
function getMouseCoords()//获得鼠标的坐标
{
    var evt = getEvent();   
    if (evt.pageX || evt.pageY)
    {
        return { x: evt.pageX, y: evt.pageY };
    }
    else
    {
        return { x: evt.clientX + document.body.scrollLeft - document.body.clientLeft, y: evt.clientY + document.body.scrollTop - document.body.clientTop };
    }
}
function go()
{

}
function getAppPath()
{
    return top.document.getElementById("hidAppPath").value;
}

function CreateDivFrm(title, ico, url, w, h)
{
    return top.ShowDivFrm(title, ico, url, w, h);
}
function getDocHeight()
{
    var height1 = document.body.clientHeight;
    var height2 = document.documentElement.clientHeight;

    var height = (height2 == 0) ? height1 : height2;

    return height;
}
function getEvent()
{ //同时兼容ie和ff的写法
    if (document.all)
        return window.event;
    func = getEvent.caller;
    while (func != null)
    {
        var arg0 = func.arguments[0];
        if (arg0)
        {
            if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation))
            {
                return arg0;
            }
        }
        func = func.caller;
    }
    return null;
}

//获得得事件触发源
function getEventSender()
{
    var evt = getEvent();
    if (document.all)
    {
        return evt.srcElement
    }
    else
    {
        return evt.target;
    }
}


//设置页面某个div高度的自适应
var docHeight;
function setDivHeight(dest, usedHeight)
{
    try
    {
        if (typeof (dest) == "string")
        {
            dest = document.getElementById(dest);
        }
        dest.style.overflow = "auto";

        if (docHeight != getDocHeight())
        {
            docHeight = getDocHeight();

            var result = docHeight - usedHeight - 5;

            result = (result < 0) ? 0 : result;

            dest.style.height = result + "px";

        }
    }
    catch (e)
    {
    }

    window.setTimeout("setDivHeight('" + dest.id + "'," + usedHeight + ")", 200);
}

function setTableHeight(dest, usedHeight)
{
    try
    {
        if (typeof (dest) == "string")
        {
            dest = document.getElementById(dest);
        }

        if (docHeight != getDocHeight())
        {

            var table = document.getElementById((document.getElementById("hidGvID").value));
            table.style.width = "100%";

            dest.style.overflow = "hidden";

            docHeight = getDocHeight();

            var tableHeight = table.clientHeight; //table的实际高度

            var validDocHeight = docHeight - usedHeight - 18; //页面上空白的可用高度

            validDocHeight = (validDocHeight < 0) ? 0 : validDocHeight;

            if (validDocHeight > tableHeight)//重直没有滚动条
            {
                dest.style.height = tableHeight + "px";
                if (dest.clientWidth < table.clientWidth)//水平出
                {
                    dest.style.overflowX = "auto";
                    dest.style.height = tableHeight + 18 + "px";
                }
            }
            else//重直有滚动条
            {
                dest.style.height = validDocHeight + "px";
                dest.style.overflowY = "auto";

                table.style.width = dest.clientWidth + "px"; //出滚动条的时候
                if (dest.clientWidth < table.clientWidth)//水平出
                {
                    dest.style.overflowX = "auto";
                }
            }
        }
    }
    catch (e)
    {
    }

    window.setTimeout("setTableHeight('" + dest.id + "'," + usedHeight + ")", 200);
}

function noselect()
{
    var evt = getEvent();
    if (evt.keyCode == 16)
    {
        document.onselectstart = new Function("return false");
    }
}
function allowselect()
{
    var evt = getEvent();
    if (evt.keyCode == 16)
    {
        document.onselectstart = null;
    }
}
// JScript 文件



function SetRefreshBtn(strRefresh)
{
    var btnRefresh = document.getElementById(strRefresh);
    if (btnRefresh != null)
    {
        top.btnRefresh = btnRefresh;
    }
}

//第一次加载时，检查那些已选中项，把它们的子项设为不可选
function CheckSelect()
{
    var ele = document.getElementsByTagName("input");
    for (var i = 0; i < ele.length; i++)
    {
        if (ele[i].type == "checkbox")
        {
            if (ele[i].checked)
            {
                MultiSelect(ele[i])
            }
        }
    }
}
//对于树状目录移动时用
function moveDir(dlgUrl, hidSelectIDs, btnRefresh)
{
    if (typeof (hidSelectIDs) == "string")
    {
        hidSelectIDs = document.getElementById(hidSelectIDs);
    }
    if (btnRefresh == null)
    {
        btnRefresh = document.getElementById(btnRefresh);
    }

    var strSelectIDs = hidSelectIDs.value;

    if (strSelectIDs == "")
    {
        alert("请选择要移动的目标!");
    }
    else
    {
        var tmpIDs = strSelectIDs.substr(1, strSelectIDs.length - 2).split(","); //去掉首尾逗号再分隔
        var arrIDs = new Array(tmpIDs.length); //可能含有ＩＤ和UpperID
        for (var i = 0; i < tmpIDs.length; i++)
        {
            arrIDs[i] = tmpIDs[i].split("|");
        }
        //判断所有的UpperID是否相同     
        var i = 0
        for (i = 0; i < tmpIDs.length - 1; i++)
        {
            if (arrIDs[i][1] != arrIDs[i + 1][1])
            {
                alert("一次只能移动同一级的目录");
                return;
            }
        }

        //重新拼装选择的ＩＤ
        strSelectIDs = ",";
        for (i = 0; i < tmpIDs.length; i++)
        {
            strSelectIDs += arrIDs[i][0] + ",";
        }

        dlgUrl = dlgUrl + "SelectIDs=" + strSelectIDs;
        var rtValue = showDlg(dlgUrl);
        if (rtValue == true)
        {
            btnRefresh.click();
        }
    }
}
function moveDoc(dlgUrl, hidSelectDocIDs, hidMyDocIDs, btnRefresh)
{
   
    var strSelectDocIDs = hidSelectDocIDs.value;

   
    var strMyDocIDs = hidMyDocIDs.value;

   
    var arrSelectDocIDs = strSelectDocIDs.split(",");

    //判断选择的ID是不是自己创建的
   
    for (var i = 0; i < arrSelectDocIDs.length; i++)
    {
        var tmp = arrSelectDocIDs[i];
        if (tmp != "")
        {
            tmp = "," + tmp + ",";
            if (strMyDocIDs.indexOf(tmp) < 0)
            {
                alert("对不起！只能移动自己创建的文档");
                return;
            }
        }
    }
    if (strSelectDocIDs == "")
    {
        alert("请选择要移动的目标!");
    }
    else
    {
        dlgUrl += "?SelectDocIDs=" + strSelectDocIDs;
        var rtValue = showDlg(dlgUrl);
        if (rtValue == true)
        {
            btnRefresh.click();
//            if (strSelectDirIDs != "")
//            {
//                parent.parent.frames[0].document.getElementById('ctl00_ContentPlaceHolder1_btnRefresh').click();
//            }
        }

    }
}

function delConfirm(hidSelect, strMessage)
{
    if (hidSelect == null)
    {
        hidSelect = document.getElementById(hidSelect);
    }
    if (hidSelect.value == "")
    {
        alert("请选择要删除的" + strMessage);
        return false;
    }
    else
    {
        var flag = confirm("确认要删除" + strMessage + "?");
        if (flag == true)
        {
            //如果是ID和UpperID的混合体，则要处理一下
            var strSelectIDs = hidSelect.value;
            var tmpIDs = strSelectIDs.substr(1, strSelectIDs.length - 2).split(","); //去掉首尾逗号再分隔
            var arrIDs = new Array(tmpIDs.length); //可能含有ＩＤ和UpperID
            for (var i = 0; i < tmpIDs.length; i++)
            {
                arrIDs[i] = tmpIDs[i].split("|");
            }

            //重新拼装选择的ＩＤ
            strSelectIDs = ",";
            for (var i = 0; i < tmpIDs.length; i++)
            {
                strSelectIDs += arrIDs[i][0] + ",";
            }
            hidSelect.value = strSelectIDs;
            return true;
        }
        else
        {
            return false;
        }
    }

}

function delDirDoc(hidSelectDir, hidSelectDoc, strMessage)
{
    if (hidSelectDir.value == "" && hidSelectDoc.value == "")
    {
        alert("请选择要删除的目标");
        return false;
    }
    else
    {
        var flag = confirm(strMessage);
        return flag;
    }
}

//显示对话框，在对话框里处理数据，不需要返回
function showDlg(dialogUrl, args)
{
    return showCustDlg(dialogUrl, args, null, null);
}

function showCustDlg(dialogUrl, args, width, height)
{
    if (width == null)
    {
        width = 270;
    }
    if (height == null)
    {
        height = 410;
    }
    var top = (window.screen.height - height) / 2;
    var left = (window.screen.width - width) / 2;
    var sFeatures = "dialogHeight:" + height + "px;dialogWidth:" + width + "px;dialogLeft:" + left + "px;dialogTop:" + top + "px;help:no;center:yes;status:no; scroll:no; ";
    return window.showModalDialog(dialogUrl, args, sFeatures);
}

function showDialog(txtID, txtName, dialogUrl)
{
    var args = new Array(2); //接受对话框返回值 

    args[0] = txtID.value;
    args[1] = txtName.value;

    if (dialogUrl.indexOf("?") > 0)
    {
        dialogUrl += "&SelectID=" + args[0];
    }
    else
    {
        dialogUrl += "?SelectID=" + args[0];
    }
    showDlg(dialogUrl, args);

    txtID.value = args[0];
    txtName.value = args[1];
    return false;
}

function showTree(txtID, txtName, dialogUrl, tableName, title, singleSelect)
{

    var args = new Array(2); //接受对话框返回值 

    args[0] = txtID.value;
    args[1] = txtName.value;

    if (dialogUrl.indexOf("?") > 0)
    {
        dialogUrl += "&SelectID=" + args[0];
    }
    else
    {
        dialogUrl += "?SelectID=" + args[0];
    }

    if (tableName != null)
    {
        dialogUrl += "&TableName=" + tableName;
    }

    if (singleSelect == true)
    {
        dialogUrl += "&SingleSelect=true";
    }
    if (title != null)
    {
        dialogUrl += "&Title=" + title;
    }
    showDlg(dialogUrl, args);
    txtID.value = args[0];
    txtName.value = args[1];

    //    if(txtName.value == "" || txtName.value == "单击选择")
    //    {
    //        txtName.style.color = "#aaaaaa";
    //        txtName.value = "单击选择";
    //    }
    //    else
    //    {
    //        txtName.style.color = "#000000";
    //    }

    return false;
}
function showDict(txtID, txtName, dialogUrl, dictItem)
{
    var args = new Array(2); //接受对话框返回值 

    args[0] = txtID.value;
    args[1] = txtName.value;

    dialogUrl += "?SelectID=" + args[0] + "&DictItem=" + dictItem;

    showDlg(dialogUrl, args);
    txtID.value = args[0];
    txtName.value = args[1];
    return false;
}
function showDeptUserTree(txtID, txtName, singleSelect, allowCheckDept)
{
    var dialogUrl = getAppPath() + "common/dialog/DeptUserDlg.aspx";
    if (allowCheckDept != null)
    {
        dialogUrl += "?AllowCheckDept=" + allowCheckDept;
    }
    showTree(txtID, txtName, dialogUrl, null, null, singleSelect);
}

function showDeptUserTreeEx(txtID, txtName)
{
    var dialogUrl = getAppPath() + "common/dialog/DeptUserEx.aspx";
    return showTree(txtID, txtName, dialogUrl, null, null, false);
}

//列表前的全选按钮用
function SelectAll(table)
{
  
    var chkSelectAll = getEventSender();
    
    for (i = 0; i < table.rows.length; i++)
    {
        var cell = table.rows[i].cells[0];
        var chkSub = cell.getElementsByTagName("INPUT")[0];
        if (chkSub == null || chkSub.type != "checkbox")
        {
            cell = table.rows[i].cells[1];
            if (cell != null)
            {
                chkSub = cell.getElementsByTagName("INPUT")[0];
            }
        }

        if (chkSub != null)
        {
            if (chkSub.checked != chkSelectAll.checked)
            {
                chkSub.click();
            }
        }
    }
}

//用于列表前面的选择，将对就的ID保存到隐藏域里
var over;
var out;
var lastChk; //最后单击的checkbox
function SetSelectID(strHidden, strID)
{
   
    var txtHidden = document.getElementById(strHidden);
    //选中的改变背景
    lastChk = getEventSender();
    var tr = GetRow(lastChk);
    while (1 == 1)
    {
        if (tr.tagName.toUpperCase() == "TR")
        {
            break;
        }
        else
        {
            tr = tr.parentNode;
        }
    }

   

    var blnChecked = lastChk.checked;
    var idValue = txtHidden.value;

    if (blnChecked)
    {
        if (idValue == "")
        {
            idValue = ",";
        }

        if (idValue.indexOf("," + strID + ",") < 0)
        {
            idValue += strID + ",";
        }
        tr.className = "selected";

        over = tr.onmouseover;
        out = tr.onmouseout;
        tr.onmouseover = null;
        tr.onmouseout = null;
    }
    else
    {
        idValue = idValue.replace("," + strID + ",", ",");
        if (idValue == ",")
        {
            idValue = "";
        }
        tr.className = "";
        if (over != null)
        {
            tr.onmouseover = over;
            tr.onmouseout = out;
        }
    }
    txtHidden.value = idValue;
}

//获得checkbox所在的行
function GetRow(chk)
{
    var tr = chk.parentNode;
    while (1 == 1)
    {
        if (tr.tagName.toUpperCase() == "TR")
        {
            break;
        }
        else
        {
            tr = tr.parentNode;
        }
    }
    return tr;
}
//在一行单击选择,可以按shift键多选
function SelectTr(strCheckBox)
{
    var evt = getEvent();
    var chk = document.getElementById(strCheckBox);
    var src = getEventSender();
    var tagName = src.tagName.toUpperCase();
    if (tagName == "TD" || tagName == "SPAN")
    {
        if (evt.shiftKey && !(lastChk.checked ^ !chk.checked))
        {
            var lastRow = GetRow(lastChk);
            var currRow = GetRow(chk);

            var lastRowIndex = lastRow.rowIndex;
            var currRowIndex = currRow.rowIndex;

            var table = currRow.parentNode;
            if (table.tagName.toLowerCase() != "table")
            {
                table = table.parentNode;
            }
            if (currRowIndex < lastRowIndex)
            {
                var tmp = lastRowIndex
                lastRowIndex = currRowIndex
                currRowIndex = tmp;
            }
            for (var i = lastRowIndex; i <= currRowIndex; i++)
            {
                var cell = table.rows[i].cells[0];
                var chkSub = cell.getElementsByTagName("INPUT")[0];
                if (chkSub == null || chkSub.type != "checkbox")
                {
                    cell = table.rows[i].cells[1];
                    chkSub = cell.getElementsByTagName("INPUT")[0];
                }
                if (lastChk.checked ^ chkSub.checked)
                {
                    chkSub.click();
                }
            }
        }
        else
        {
            chk.click();
        }
    }
}

//自定义选择（全选、反选、不选）
function CustSelect(status)
{
    var table = document.getElementById(document.getElementById("hidGvID").value);

    switch (status)
    {
        case 0: //全部
            for (i = 0; i < table.rows.length; i++)
            {
                var chk = table.rows[i].cells[0].getElementsByTagName("INPUT")[0];
                if (chk == null || chk.type != "checkbox")
                {
                    try
                    {
                        chk = table.rows[i].cells[1].getElementsByTagName("INPUT")[0];
                    }
                    catch (e)
                    {
                        continue;
                    }
                }
                if (!chk.checked)
                {
                    chk.click();
                }
            }
            break;
        case 1: //反选
            for (i = 1; i < table.rows.length; i++)
            {
                var chk = table.rows[i].cells[0].getElementsByTagName("INPUT")[0];
                if (chk == null || chk.type != "checkbox")
                {
                    try
                    {
                        chk = table.rows[i].cells[1].getElementsByTagName("INPUT")[0];
                    }
                    catch (e)
                    {
                        continue;
                    }
                }
                chk.click();
            }
            break;
        case 2: //不选
            for (i = 0; i < table.rows.length; i++)
            {
                var chk = table.rows[i].cells[0].getElementsByTagName("INPUT")[0];
                if (chk == null || chk.type != "checkbox")
                {
                    try
                    {
                        chk = table.rows[i].cells[1].getElementsByTagName("INPUT")[0];
                    }
                    catch (e)
                    {
                        continue;
                    }
                }
                if (chk.checked)
                {
                    chk.click();
                }
            }
            break;
    }
}

function ValidateOrders(txt)
{
    var reg = new RegExp("^-?\\d*$");
    if (reg.test(txt.value) == false)
    {
        alert("数字格式不对");
        txt.focus();
        txt.select();
    }
}

//用于排序
function SetOrders(hidModifyIDs, strID, txtValue)
{
    if (hidModifyIDs.value == "")
    {
        hidModifyIDs.value = ",";
    }


    var strIDValue = "," + strID + "|" + txtValue.value;
    var strModifyIDs = hidModifyIDs.value;

    if (strModifyIDs.indexOf("," + strID + "|") > -1)//判断原来里面有没有
    {
        var regexp = new RegExp("," + strID + "\\x7c\\w*"); //"x7c" 是 "|" 的16进制
        hidModifyIDs.value = strModifyIDs.replace(regexp, strIDValue);
    }
    else
    {
        hidModifyIDs.value += strID + "|" + txtValue.value + ",";
    }
}



function Collapse(RowIndex, SubRows)
{

    var img = getEventSender();
    var display; //显示还是隐藏
    //改变图标
    var imgUrl = img.src;

    if (imgUrl.substr(imgUrl.length - 5, 1) == "+")
    {
        imgUrl = imgUrl.substr(0, imgUrl.length - 5) + "-" + imgUrl.substr(imgUrl.length - 4, 4);  
        display = true;
    }
    else
    {
        imgUrl = imgUrl.substr(0, imgUrl.length - 5) + "+" + imgUrl.substr(imgUrl.length - 4, 4);  
        display = false;
    }
    img.src = imgUrl;


    var table = img.parentNode;
    while (table.tagName.toUpperCase() != "TABLE")//获得TABLE对象
    {
        table = table.parentNode;
    }


    var srcTr = table.rows[RowIndex]; //发生事件的行
    RowIndex++; //表头不参与计算

    for (i = RowIndex; i < RowIndex + SubRows; i++)
    {
        var trUpper = document.getElementById(table.rows[i].getAttribute("UpperID"));

        if (display == true)//还原原子节点状态
        {

            if (srcTr == trUpper)
            {
                table.rows[i].style.display = "";
                table.rows[i].flag = "";

            }
            else
            {
                if (table.rows[i].flag == null)
                {
                    table.rows[i].flag = "";
                }
                if (trUpper.style.display != "none")
                {
                    table.rows[i].style.display = table.rows[i].flag;
                }
            }
        }
        else
        {
            if (srcTr == trUpper)//如果是直接下级则加上标志
            {
                table.rows[i].flag = "none";
            }
            table.rows[i].style.display = "none";

            if (table.rows[i].flag == null)
            {
                table.rows[i].flag = "";
            }
        }
    }

    SetHideTrID(table); //把所有隐藏行的ID放进隐藏域
}
function SetHideTrID(table)
{
    var hidIDs = document.getElementById(document.getElementById("hiddenClientID").value);
    var strIDs = ",";
    for (i = 1; i < table.rows.length; i++)
    {
        var row = table.rows[i];
        var id = row.id;
        var flag = row.flag;
        if (flag == null)
        {
            flag = "";
        }
        var display = row.style.display;
        if (display == "none")
        {
            strIDs += id + "|" + flag + ",";
        }
    }
    hidIDs.value = strIDs;
}

function GetDivHeight()
{
    var table = document.getElementById((document.getElementById("hidGvID").value));
    var hidUsedHeight = document.getElementById("hidUsedHeight"); //页面上除列表以外的界面

    var otherHeight = 50;
    if (hidUsedHeight != null)
    {
        otherHeight = hidUsedHeight.value;
    }
    if (table == null)//如果没有记录，则该GridView的ID将不存在。
    {
        return 30;
    }

    var tableHeight = table.clientHeight + 2;
    var bodyHeight = document.body.clientHeight - otherHeight;
    if (tableHeight < bodyHeight)
    {
        return tableHeight;
    }
    else
    {
        return bodyHeight;
    }
}

//实现安钮单击
function ClickBtn(strBtn)
{
    document.getElementById(strBtn).click();
}

//修改左边模块名称
function SetModuleName(id, name)
{
    var win = top.frames["frameLeft"];
    if (win == null)
    {
        win = top;
    }
    var module = win.document.getElementById("span" + id);

    if (module)
    {
        module.innerHTML = name;
    }
}

function resizeImage(img, maxWidth, maxHeight, containerHeight)
{
    var width = img.width;
    var height = img.height;
    var ratio = width / height;
    var maxRatio = maxWidth / maxHeight;
    if (ratio > maxRatio)
    {
        if (width > maxWidth)
        {
            width = maxWidth;
            height = maxWidth / ratio;

        }
    }
    else
    {
        if (height > maxHeight)
        {
            height = maxHeight;
            width = maxHeight * ratio;
        }
    }

    img.width = width;
    img.height = height;

    if (containerHeight != null)
    {
        img.style.marginTop = ((containerHeight - height) / 2) + "px";
    }

   
}

function loadImage(url, container, maxWidth, maxHeight, containerHeight)
{
    if (typeof (container) == "string")
    {
        container = document.getElementById(container);
    }

    if (url == null || url == "")
    {
        container.innerHTML = "<div style='width:" + maxWidth + "px;height:" + maxHeight + "px;line-height:" + maxHeight + "px;'>暂无图片</div>";
        return;
    }
    else
    {
        container.innerHTML = "<div style='width:" + maxWidth + "px;height:" + maxHeight + "px;line-height:" + maxHeight + "px;'>正在加载</div>";
    }

    var img = new Image();
    img.border = 0;
    

    img.onload = function()
    {
        resizeImage(this, maxWidth, maxHeight, containerHeight);
        container.innerHTML = "";
        container.appendChild(this);
    }

    img.onerror = function()
    {
        container.innerHTML = "<div style='width:" + maxWidth + "px;height:" + maxHeight + "px;line-height:" + maxHeight + "px;'>加载失败</div>";
    }

    img.src = url;//一定要写在后面



}